package controller;

import BBS.bean.UserInfo;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/user")
public class UserController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        if (null == type) {
            response.sendRedirect("index.jsp");
        }

        if ("login".equals(type)) {
            login(request, response);
        }
        if ("add".equals(type)){
            save(request,response);
        }

    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setUserPassword(userPassword);
        UserService service = new UserServiceImpl(request.getServletContext());
        userInfo = service.findUserByUser(userInfo);

        if (null != userInfo) {
            request.getSession().setAttribute("USER_INFO", userInfo);
            response.sendRedirect("check.jsp?msg=ok");
        } else {
            response.sendRedirect("check.jsp?msg=error");
        }
    }

    private void save(HttpServletRequest  request,HttpServletResponse  response)  throws ServletException, IOException{
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String userNickName =  request.getParameter("userNickName");
        String email =  request.getParameter("email");
        String phone =  request.getParameter("phone");
        String sex =  request.getParameter("sex");
        String date = request.getParameter("date");

        UserInfo ui = new UserInfo();
        ui.setUserName(userName);
        ui.setUserPassword(userPassword);
        ui.setUserNickName(userNickName);
        ui.setEmail(email);
        ui.setPhone(phone);
        ui.setSex(sex);
        ui.setBirthday(date);

        UserService us = new UserServiceImpl(request.getServletContext());
        int i = us.checkUserName(ui);

        if (null != userName){
            if (i==1){
                response.sendRedirect("register.jsp");
            }else{
                us.saveUser(ui);
                response.sendRedirect("index.jsp");
            }
        }else{
            response.sendRedirect("register.jsp");
        }

    }
}
